package cn.wedding.dao.impl;

import cn.wedding.entity.Admin;
import cn.wedding.dao.AdminDao;
import cn.wedding.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminImpl implements AdminDao {
    @Override
    //管理员登录
    public Admin login(String adminName, String password) {
        BaseDao baseDao = new BaseDao();
        Connection connection = baseDao.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;
        String sql = "select * from admin where adminname=? and adminpassword=?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,adminName);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if (rs.next()){
                admin = new Admin(rs.getInt(1),rs.getString(2),rs.getString(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            baseDao.closeAll(rs,ps,connection);
        }
        return admin;
    }
}
